package com.niit.drugsmanager.controller;

import com.niit.drugsmanager.entity.RealNameAuth;
import com.niit.drugsmanager.entity.User;
import com.niit.drugsmanager.entity.UserLog;
import com.niit.drugsmanager.service.RealNameService;
import com.niit.drugsmanager.service.UserLogService;
import com.niit.drugsmanager.service.UserService;
import com.niit.drugsmanager.utils.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.time.LocalDateTime;
import org.springframework.web.bind.support.SessionStatus;

import jakarta.servlet.http.HttpSession;

@Controller
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserLogService userLogService;
    @Autowired
    private RealNameService realNameService;

    @GetMapping("/login")
    public String login(Model model) {
        return "/user/login";
    }

    @PostMapping("/login")
    public String login(
            @RequestParam("email") String email,
            @RequestParam("password") String password,
            Model model,
            RedirectAttributes redirectAttributes,
            HttpSession session) {
        User result = userService.getUser(email, password);
        if(result != null) {
            UserLog userLog = new UserLog();
            userLog.setTime(LocalDateTime.now());
            userLog.setUser(result);
            userLog.setLogin(true);
            userLogService.addUserLog(userLog);
            RealNameAuth realNameAuthByUserId = realNameService.getRealNameAuthByUserId(result.getId());
            if(realNameAuthByUserId != null) {
                session.setAttribute("avatar", realNameAuthByUserId.getAvatarBase64());
            }
            session.setAttribute("user", result);
            if(!result.hasRole(0b0001)) return "redirect:/api/user/ban";
            return "redirect:/api/drugs";
        }else {
            redirectAttributes.addFlashAttribute("errorMessage", "登录失败");
            return "redirect:/api/user/login";
        }
    }

    @PostMapping("/logout")
    public String logout(
            RedirectAttributes redirectAttributes,
            HttpSession session) {
        User user = (User) session.getAttribute("user");
        String s = SessionUtils.ifUserNull(user, userService);
        if(s != null) return s;
        UserLog userLog = new UserLog();
        userLog.setTime(LocalDateTime.now());
        userLog.setUser(user);
        userLog.setLogin(false);
        userLogService.addUserLog(userLog);
        session.invalidate();
        return "redirect:/api/user/login";
    }

    @PostMapping("/register")
    public String register(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            @RequestParam("email") String email,
            @RequestParam("phone") String phone,
            RedirectAttributes redirectAttributes) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setPhone(phone);
        user.setRole(0b0011);
        user.setPermission(0b1100);
        User result = userService.createUser(user);
        if (result == null) {
            redirectAttributes.addFlashAttribute("errorMessage", "注册失败");
        }
        return "redirect:/api/user/login";
    }

    @PostMapping("/log")
    public String log(Model model, HttpSession session){
        //登录检查
        User user = (User) session.getAttribute("user");
        String s = SessionUtils.ifUserNull(user, userService);
        if(s != null) return s;

        //权限检查
        boolean flag1 = user.hasRole(0b0101);
        boolean flag2 = user.hasPermission(0b0010);
        if(!(flag1 && flag2)) return "redirect:/api/user/non-permission";

        model.addAttribute("userLogs", userLogService.getAllUserLog());
        return "/user/log";
    }

    @GetMapping("/non-permission")
    public String haveNotPermission(Model model, HttpSession session) {
        //登录检查
        User user = (User) session.getAttribute("user");
        String s = SessionUtils.ifUserNull(user, userService);
        if(s != null) return s;

        return "/non-permission";
    }
    @GetMapping("/ban")
    public String isBan(Model model, HttpSession session) {
        //登录检查
        User user = (User) session.getAttribute("user");
        String s = SessionUtils.ifUserNull(user, userService);
        if(s != null) return s;

        return "/ban";
    }
}
